//
//  LXConcernListViewController.m
//  cat
//
//  Created by imera on 16/3/31.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXConcernListViewController.h"
#import "LXNavBarView.h"
#import "LXConcernUserInfoCell.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "LXUserInfoViewController.h"

@interface LXConcernListViewController () <LXNavBarViewDelegate, UITableViewDataSource, UITableViewDelegate, LXConcernUserInfoCellDelegate>

@property (nonatomic, weak) UITableView *tableView;

/** 关注的人列表 */
@property (nonatomic, strong) NSArray *concernUserArr;

@end

@implementation LXConcernListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self initSubViews];
    
    [self getConcernUserList];
}

#pragma mark 
- (void)getConcernUserList
{
    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 2.发送请求
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/getFollow" parameters:@{@"userid":@(self.user.ID)} success:^(AFHTTPRequestOperation *operation, id response) {
        NSArray *arr = response;
        if ([response isKindOfClass:[NSDictionary class]]) {
            self.concernUserArr = [NSArray array];
            [self.tableView reloadData];
        }else{
            NSMutableArray *userArr = [NSMutableArray array];
            for (NSDictionary *dict in arr) {
                LXUser *user = [LXUser initWithDict:dict];
                [userArr addObject:user];
            }
            self.concernUserArr = userArr;
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"您的网络开小差了..."];
    }];
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"关注的人";
    navBarView.myDelegate = self;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];
    
    UITableView *tableView = [[UITableView alloc]init];
    tableView.frame = CGRectMake(0, 20 + LXNavBarHeight, self.view.frame.size.width, self.view.frame.size.height - 20 - LXNavBarHeight);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
}

#pragma mark - tableview数据源和代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    LXConcernUserInfoCell *userInfoCell = [[LXConcernUserInfoCell alloc]init];
    userInfoCell.user = self.concernUserArr[indexPath.row];
    userInfoCell.title = @"已关注";
    userInfoCell.MyDelegate = self;
    [cell addSubview:userInfoCell];
    userInfoCell.frame = CGRectMake(0, 0, self.view.frame.size.width, 70);

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.concernUserArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXUserInfoViewController *userInfoVc = [[LXUserInfoViewController alloc]init];
    userInfoVc.user = self.concernUserArr[indexPath.row];
    userInfoVc.isEdit = NO;
    [self.navigationController pushViewController:userInfoVc animated:YES];
}

#pragma mark - 自定义导航栏代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 关注
- (BOOL)concernUserInfoCell:(LXConcernUserInfoCell *)concernUserInfoCell title:(NSString *)title
{
    //获取数据
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSNumber *userId = @(self.user.ID);
    NSNumber *followId = @(concernUserInfoCell.user.ID);
    
    if([title isEqualToString:@"关注"]){
        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/setFollow";
        
        [mgr POST:url parameters:@{@"userid":userId, @"followid":followId} success:^(AFHTTPRequestOperation *operation, id response) {
            NSDictionary *dict = response;
            if([dict[@"successed"] integerValue] == 1){
                [MBProgressHUD showSuccess:@"关注成功"];
            }else{
                [MBProgressHUD showSuccess:@"关注失败"];
            }
        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            [MBProgressHUD showError:@"您的网络开小差了..."];
        }];
    }else{
        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/cancelFollow";
        
        [mgr POST:url parameters:@{@"userid":userId, @"followid":followId} success:^(AFHTTPRequestOperation *operation, id response) {
            NSDictionary *dict = response;
            
            if([dict[@"successed"] integerValue] == 1){
                [MBProgressHUD showSuccess:@"取消关注成功"];
            }else{
                [MBProgressHUD showSuccess:@"取消关注失败"];
            }
        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            [MBProgressHUD showError:@"您的网络开小差了..."];
        }];
    }
    return YES;
}
@end
